package com.dingwen.stusprbooes.controller;

import com.dingwen.stusprbooes.entity.GoodsEntity;
import com.dingwen.stusprbooes.service.GoodsService;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * document controller
 *
 * @author dingwen
 * 2021/6/27 21:19
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {
    private final GoodsService goodsService;

    @Autowired
    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }


    /**
     * 保存
     * TODO 同意异常处理，返回结果。日志
     *
     * @return {@link Object}
     */
    @PostMapping("")
    public Object save() {
        BulkResponse bulkItemResponses = null;
        try {
            bulkItemResponses = goodsService.save();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bulkItemResponses;

    }

    /**
     * 删除
     * TODO 同意异常处理，返回结果。日志
     *
     * @return {@link Object}
     */
    @DeleteMapping("")
    public Object remove() {
        try {
            return goodsService.removeById("FCS8TXoBJFoDj88raS_T");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @GetMapping("/search")
    public Object search(@RequestParam("keywords") String keywords,
                         @RequestParam("pageNum") Integer pageNum,
                         @RequestParam("pageSize") Integer pageSize) {
        try {
            return goodsService.searchPageForHighlight(keywords, pageNum, pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
